//
//  RegisterViewController.m
//  IGOtest
//
//  Created by administrator on 14-2-24.
//  Copyright (c) 2014年 administrator. All rights reserved.
//

#import "RegisterViewController.h"
#define KIPADD @"10.207.1.23:8888/user/add.php"

@interface RegisterViewController ()<UITextFieldDelegate>

@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.passwordfild setSecureTextEntry:YES];
    [self.passwordfildagin setSecureTextEntry:YES];
    
    self.usernamefild.delegate = self;
    self.passwordfild.delegate=self;
    self.passwordfildagin.delegate=self;
    
    self.usernamefild.tag=1;
    self.passwordfild.tag=2;
    self.passwordfildagin.tag=3;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 1:
            if ([self isUsername:self.usernamefild.text]==YES)
            {
                self.usernameimage.image=[UIImage imageNamed:@"对.png"];
            }
            else
            {
                self.usernameimage.image=[UIImage imageNamed:@"错.png"];
            }
            break;
        case 2:
            if ([self isPassword:self.passwordfild.text]==YES)
            {
                self.passwordimage.image=[UIImage imageNamed:@"对.png"];
            }
            else
            {
                self.usernameimage.image=[UIImage imageNamed:@"错.png"];
            }
        case 3:
            if ([self.passwordfild.text isEqual:self.passwordfildagin.text]==YES)
            {
                self.passwordimageagin.image=[UIImage imageNamed:@"对.png"];
            }
            else
            {
                self.passwordimageagin.image=[UIImage imageNamed:@"错.png"];
            }
        default:
            break;
    }
}

//判断姓名有效性
-(BOOL)isUsername:(NSString *)name
{
    NSString *regex = @"[A-Z0-9a-z_]{6,20}";
    NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [nameTest evaluateWithObject:name];
}
//判断密码有效性
-(BOOL)isPassword:(NSString *)password
{
    NSString *regex = @"[A-Z0-9a-z_]{6,20}";
    NSPredicate *passwdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [passwdTest evaluateWithObject:password];
}


- (IBAction)returnpage:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)sure:(UIButton *)sender {
    NSString *usestr=[NSString stringWithFormat:@"?username1=%@&password1=%@",self.usernamefild.text,self.passwordfild.text];
    NSURL *url=[NSURL URLWithString:[KIPADD stringByAppendingString:usestr]];
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
    NSError *error=nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
//    NSString *response=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//    NSString=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding ];
    
    NSLog(@"%@",str);
      if([str isEqualToString:@"insert success!"])
       {
           UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil ];
           [alter show];
        NSLog(@"成功！");
  
       }
       else{
           UIAlertView *alters = [[UIAlertView alloc]initWithTitle:@"提示" message:@"失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil ];
           [alters show];
        NSLog(@"失败！");
       }
  

}
@end
